package edu.tgc.service.system;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.abyss.common.annotation.Log;
import org.abyss.common.service.CommonCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.tgc.entity.system.SysDept;
import edu.tgc.entity.system.SysDuty;
import edu.tgc.entity.system.SysPosition;
import edu.tgc.repository.system.SysPositionRepository;

@Log(value = "岗位")
@Service
public class SysPositionService extends CommonCrudService<SysPosition, UUID> {

    @Autowired
    protected SysPositionRepository sysPositionRepository;

    public void setDepts(UUID id, Set<String> deptIds) {
        SysPosition entity = this.baseRepository.findOne(id);
        HashSet<SysDept> entitys = new HashSet<>();
        if (deptIds != null) {
            for (String entityId : deptIds) {
                entitys.add(new SysDept(UUID.fromString(entityId)));
            }
        }
        entity.setDepts(entitys);
        this.baseRepository.saveAndFlush(entity);
    }

    public void setDuties(UUID id, Set<String> dutyIds) {
        SysPosition entity = this.baseRepository.findOne(id);
        HashSet<SysDuty> entitys = new HashSet<>();
        if (dutyIds != null) {
            for (String entityId : dutyIds) {
                entitys.add(new SysDuty(UUID.fromString(entityId)));
            }
        }
        entity.setDuties(entitys);
        this.baseRepository.saveAndFlush(entity);
    }
}
